package com.framework.view.indicator.indicators;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

import com.framework.view.indicator.IPageIndicator;
import com.framework.view.indicator.indicators.configs.WrapIndicatorConfig;
import com.framework.view.indicator.model.IndicatorShapeType;
import com.framework.view.indicator.model.PositionInfo;
import com.framework.view.indicator.util.ArgbEvaluatorHolder;
import com.framework.view.indicator.CalculationHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 包裹内容的指示器
 *
 * @author liangzx
 * @version 1.0
 * @time 2020-01-07 17:49
 **/
public class WrapIndicatorView extends View implements IPageIndicator<WrapIndicatorConfig> {

    /**
     * 指示器配置
     */
    private WrapIndicatorConfig mConfig;
    /**
     * 绘制区域
     */
    private RectF mWrapRect = new RectF();
    /**
     * 标题位置信息列表
     */
    private List<PositionInfo> mPositions;
    /**
     * 画笔
     */
    private Paint mPaint;

    public WrapIndicatorView(Context context) {
        super(context);
    }

    @Override
    public void init(WrapIndicatorConfig config, int totalCount, int position) {
        mConfig = config;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (config.isStroke()) {
            mPaint.setStrokeWidth(config.getStrokeWidth());
            mPaint.setStyle(Paint.Style.STROKE);
        } else {
            mPaint.setStyle(Paint.Style.FILL);
        }
        mPaint.setColor(config.getColor(position));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mConfig.getShapeType() == IndicatorShapeType.ROUND_RECT) {
            canvas.drawRoundRect(mWrapRect, mConfig.getRoundRadius(), mConfig.getRoundRadius(), mPaint);
        } else {
            canvas.drawRect(mWrapRect, mPaint);
        }
    }

    @Override
    public void updateTitlePositions(List<PositionInfo> positions) {
        boolean isInit = null == mPositions || mPositions.isEmpty();
        mPositions = positions;
        if (isInit) {
            updateRect(0);
        }
    }

    /**
     * 更新绘制区域
     *
     * @param position
     */
    private void updateRect(int position) {
        mPaint.setColor(mConfig.getColor(position));
        if (null == mPositions || mPositions.isEmpty()) {
            return;
        }
        final PositionInfo positionInfo;
        if (position >= mPositions.size()) {
            positionInfo = mPositions.get(mPositions.size() - 1);
        } else if (position < 0) {
            positionInfo = mPositions.get(0);
        } else {
            positionInfo = mPositions.get(position);
        }
        mWrapRect.top = positionInfo.contentTop - mConfig.getPendingTop();
        mWrapRect.bottom = positionInfo.contentBottom + mConfig.getPendingBottom();

        mWrapRect.left = positionInfo.contentLeft - mConfig.getPendingLeft();
        mWrapRect.right = positionInfo.contentRight + mConfig.getPendingRight();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (null == mPositions || mPositions.isEmpty()) {
            return;
        }
        // 计算颜色
        if (mConfig.isMultiColors()) {
            int color;
            if (mConfig.isColorFollowSlideChange()) {
                final ArrayList<Integer> colors = mConfig.getColors();
                int currentColor = colors.get(Math.abs(position) % colors.size());
                int nextColor = colors.get(Math.abs(position + 1) % colors.size());
                color = ArgbEvaluatorHolder.eval(positionOffset, currentColor, nextColor);
            } else {
                color = mConfig.getColor(position);
            }
            mPaint.setColor(color);
        }

        // 计算锚点位置
        PositionInfo current = CalculationHelper.getImitativePosition(mPositions, position);
        PositionInfo next = CalculationHelper.getImitativePosition(mPositions, position + 1);

        float leftX;
        float nextLeftX;
        float rightX;
        float nextRightX;

        leftX = current.contentLeft - mConfig.getPendingLeft();
        nextLeftX = next.contentLeft - mConfig.getPendingLeft();
        rightX = current.contentRight + mConfig.getPendingRight();
        nextRightX = next.contentRight + mConfig.getPendingRight();

        mWrapRect.left = leftX + (nextLeftX - leftX) * mConfig.getStartInterpolator().getInterpolation(positionOffset);
        mWrapRect.right = rightX + (nextRightX - rightX) * mConfig.getEndInterpolator().getInterpolation(positionOffset);
        invalidate();
    }

    @Override
    public void onPageSelected(int position) {
        updateRect(position);
        invalidate();
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onEntering(int index, int totalCount, float enterPercent, boolean leftToRight) {
    }

    @Override
    public void onLeaving(int index, int totalCount, float leavePercent, boolean leftToRight) {
    }

    @Override
    public void onSelected(int index, int totalCount) {
    }

    @Override
    public void onCancelSelected(int index, int totalCount) {
    }

//    @Override
//    protected void onDetachedFromWindow() {
//        super.onDetachedFromWindow();
//        if (null != mPositions) {
//            mPositions.clear();
//        }
//    }
}
